﻿using supermarket.method;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace supermarket
{
    public partial class supselectuser : Form
    {
        Classuser us = new Classuser();
        Classdvclear dvc = new Classdvclear();
        public supselectuser ()
        {
            InitializeComponent();
            dv.RowCount = 100;
        }

        private void comboBox1_SelectedIndexChanged ( object sender, EventArgs e )
        {

        }

        private void textBox1_TextChanged ( object sender, EventArgs e )
        {
            if (comcondition.Text == "用户名")
            {
                dvc.dvclear(dv, 5);
                us.selectuserinfotodv(dv, txtusername.Text, "");
            }
            else if (comcondition.Text == "手机号")
            {
                dvc.dvclear(dv, 5);
                us.selectuserinfotodv(dv, "", txtusername.Text);
            }
            else
            {
                MessageBox.Show("请选择查询条件！");
            }

        }

        private void button1_Click ( object sender, EventArgs e )
        {
            dvc.dvclear(dv, 5);
            us.selectuserinfotodv(dv, txtusername.Text, "");
        }

        private void button2_Click ( object sender, EventArgs e )
        {
            // 选择保存路径
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel Files|*.xlsx";
            saveFileDialog.Title = "Save an Excel File";
            saveFileDialog.FileName = "Export.xlsx";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;

                // 查询数据表
                DataTable dataTable = us.getuserall();

                // 导出到Excel
                if (ExcelHelper.ExportToExcel(dataTable, filePath))
                {
                    MessageBox.Show("导出成功！");
                }
                else
                {
                    MessageBox.Show("导出失败！");
                }
            }
        }

        private void supselectuser_Load ( object sender, EventArgs e )
        {
            dvc.dvclear(dv, 5);
            us.selectuserinfotodv(dv, "", "");
        }
    }
}
